package com.bishe.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.util.StrUtil;
import com.api.EcoBootException;
import com.api.Result;
import com.api.entitis.model.LoginModel;
import com.api.entitis.model.UserModel;
import com.api.service.IAdminService;
import com.api.service.ICompanyService;
import com.api.service.IUserService;
import com.api.util.CompanyStpUtil;
import com.api.util.JobUtils;
import com.api.util.UserStpUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 管理员控制层
 * @date 2022/02/18 27:11:38
 */
@RestController
@CrossOrigin
@RequestMapping("/admin")
@Api(tags = "管理员控制层")
public class AdminController {
    @Autowired
    private IAdminService adminService;

    @Resource
    private ICompanyService companyService;

    @Autowired
    private IUserService userService;

    @ApiOperation("admin登录")
    @PostMapping("/login")
    public Result login(@RequestBody LoginModel loginModel){

        JobUtils.checkLoginModel(loginModel);

        UserModel login = adminService.login(loginModel);

        if (login!=null){
            return Result.ok("ok",login);
        }

        return Result.error("用户名密码错误!");
    }

    @ApiOperation("根据id删除用户")
    @SaCheckLogin
    @DeleteMapping("/delete/user/{id}")
    public Result removeUserById(@PathVariable String id){
        if (StrUtil.isNotEmpty(id)){
            userService.removeById(id);
        }
        return Result.ok();
    }

    @ApiOperation("根据id删除企业")
    @SaCheckLogin
    @DeleteMapping("/delete/company/{id}")
    public Result removeCompanyById(@PathVariable String id){
        if (StrUtil.isNotEmpty(id)){
            companyService.removeById(id);
        }
        return Result.ok();
    }

    @ApiOperation("退出登录")
    @PostMapping("/loginOut")
    public Result loginOut( ){
        StpUtil.logout();
        CompanyStpUtil.logout();
        UserStpUtil.logout();

        return Result.ok();
    }

    @ApiOperation("是否登录")
    @PostMapping("/isLogin")
    public Result isLogin( ){
        JobUtils.checkLogin(); //检查登录

        return Result.ok();
    }
}
